package com.market.order.server.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.market.common.core.domain.Result;
import com.market.common.security.service.TokenService;
import com.market.order.common.domain.vo.VoOrder;
import com.market.order.server.config.MqConfig;
import com.market.order.server.mapper.TOrderDetailMapper;
import io.jsonwebtoken.lang.Collections;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.market.order.server.mapper.TOrderMapper;
import com.market.order.common.domain.TOrder;
import com.market.order.server.service.ITOrderService;

/**
 * 订单Service业务层处理
 *
 * @author market
 * @date 2024-02-26
 */
@Service
public class TOrderServiceImpl implements ITOrderService
{
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TOrderDetailMapper tOrderDetailMapper;

    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public TOrder selectTOrderByOrderId(Integer orderId)
    {
        return tOrderMapper.selectTOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param tOrder 订单
     * @return 订单
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder)
    {
        return tOrderMapper.selectTOrderList(tOrder);
    }

    /**
     * 新增订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int insertTOrder(TOrder tOrder)
    {
        return tOrderMapper.insertTOrder(tOrder);
    }

    /**
     * 修改订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int updateTOrder(TOrder tOrder)
    {
        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByOrderIds(Integer[] orderIds)
    {
        Integer i=tOrderMapper.deleteTOrderByOrderIds(orderIds);
        if (i>0){
                 tOrderDetailMapper.deleteByOrderId(orderIds[0]);

        }

        return i;
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByOrderId(Integer orderId)
    {
        return tOrderMapper.deleteTOrderByOrderId(orderId);
    }

    @Override
    public List<TOrder> selectOrderList(VoOrder voOrder) {
        return tOrderMapper.selectOrderList(voOrder);
    }

    @Override
    public Result cancel(Integer orderId) {
        Integer i=tOrderMapper.cancel(orderId);
        return Result.success(i,"订单取消成功");
    }

    @Override
    public Result updateOrderStatus(Integer orderId, Integer orderStatus) {
        Integer i=tOrderMapper.updateOrderStatus(orderId,orderStatus);
        return Result.success(i,"订单状态修改成功");
    }

    @Override
    public Result sendMsg(String phone) {
        rabbitTemplate.convertAndSend(MqConfig.MESSAGE_EXCHANGE,MqConfig.MESSAGE_ROUTE_KEY,phone,
               message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
               },new CorrelationData(UUID.randomUUID().toString()));
        return Result.success(null,"短信发送成功");
    }

    @Override
    public Result addOrder(TOrder tOrder) {
        tOrderMapper.insertTOrder(tOrder);
        return Result.success(tOrder.getOrderId());
    }

    @Override
    public Map<String, List<TOrder>> queryUserByIdBatch(ArrayList<UserWrapBatchService.Request> userReqs) {
        List<Long> collect = userReqs.stream().map(UserWrapBatchService.Request::getUserId).collect(Collectors.toList());

        List<TOrder> list=tOrderMapper.selectAll(collect);

        Map<Integer, List<TOrder>> listMap = list.stream().collect(Collectors.groupingBy(TOrder::getOrderId));

        HashMap<String, List<TOrder>> map = new HashMap<>();

           userReqs.forEach(val->{
               List<TOrder> tOrderList = listMap.get(val.getUserId());
               if (!Collections.isEmpty(tOrderList)){
                     map.put(val.getRequestId(),list);
               }else {
                   map.put(val.getRequestId(), null);
               }

           });


        return map;
    }


}
